package p1300;

import java.util.ArrayList;
import java.util.List;

/**
 * @author pengjz <br>
 * @version 1.0 <br>
 * @date 2022/2/15 10:46 <br>
 */
public class Leetcode_1380 {

    public static void main(String[] args) {
        final Leetcode_1380 l = new Leetcode_1380();
        System.out.println(
                l.luckyNumbers(new int[][]{{3, 7, 8}, {9, 11, 13}, {15, 16, 17}})
        );
    }

    public List<Integer> luckyNumbers(int[][] matrix) {
        List<Integer> result = new ArrayList<>();
        for (int[] ints : matrix) {
            for (int j = 0; j < ints.length; j++) {
                if (isMin(ints[j], ints)) {
                    int[] column = new int[matrix.length];
                    for (int k = 0; k < matrix.length; k++) {
                        column[k] = matrix[k][j];
                    }
                    if (isMax(ints[j], column)) {
                        result.add(ints[j]);
                    }
                    break;
                }
            }
        }
        return result;
    }

    private boolean isMin(int a, int[] ints) {
        for (int anInt : ints) {
            if (anInt < a) {
                return false;
            }
        }
        return true;
    }

    private boolean isMax(int a, int[] ints) {
        for (int anInt : ints) {
            if (anInt > a) {
                return false;
            }
        }
        return true;
    }
}
